window.fi = {};
//立即执行函数
(function() {
  //数组封装成列表
  //一般的函数以小写字母开头

  function list() {
    this.dataStore = [];
    this.listSize = 0;
    this.pos = 0;
  }

  list.prototype.clear = function(element, after) {
    delete this.dataStore;
    this.dataStore = [];
    this.pos = 0;
    this.listSize = 0;
  };

  list.prototype.insert = function(element, after) {
    //判断插入的位置是否合格
    if (after >= 0 && after < this.listSize) {
      this.dataStore.splice(after, 0, element);
      this.listSize++;
      return true;
    }
    return false;
  };

  list.prototype.append = function(element) {
    this.dataStore[this.listSize++] = element;
    return true;
    // var temp=this.listSize;
    // this.listSize++
    //this.dataStore[++this.listSize]=element;
    // this.listSize++
    // var temp=this.listSize;
  };

  list.prototype.find = function(element) {
    var index = -1;
    for (var i = 0; i < this.listSize; i++) {
      if (element === this.dataStore[i]) {
        index = i;
        break;
      }
    }
    return index;
  };

  list.prototype.remove = function(element) {
    //确定是哪个一个元素
    var index = this.find(element);
    if (index != -1) {
      this.dataStore.splice(index, 1);
      this.listSize--;
      return true;
    }
    return false;
  };

  list.prototype.length = function(element) {
    return this.listSize;
  };

  //遍历的方法
  list.prototype.front = function() {
    this.pos = 0;
  };

  list.prototype.end = function() {
    this.pos = this.listSize - 1;
  };

  list.prototype.prev = function() {
    if (this.pos > 0) {
      this.pos--;
      return true;
    }
    return false;
  };

  list.prototype.next = function() {
    if (this.pos < this.listSize - 1) {
      this.pos++;
      return true;
    }
    return false;
  };

  list.prototype.cur = function() {
    return this.pos;
  };

  list.prototype.getElement = function() {
    return this.dataStore[this.pos];
  };

  list.prototype.removeCur = function() {
    this.dataStore.splice(this.pos, 1);
    this.pos--;
  };

  list.prototype.foreach = function(callback) {
    // for (list.front(); list.cur() < list.length(); list.next()) {
    //     console.log(list.getElement())
    // }
    this.front();
    var element = null;
    do {
      element = this.getElement();
      callback(this.pos, element);
    } while (this.next());
  };

  //构造函数当类用（大写开头）
  window.fi.List = list;
})();
